﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Revis.VClinicNet.App.Internal;
using Revis.VClinicNet.App.Internal.Dto;
using Revis.VClinicNet.App.Internal.Dto.Validation;
using Revis.VClinicNet.UI.Internal.Mvc.Infrastructure;
using Revis.VClinicNet.Core.Domain.Credentials;
using Revis.VClinicNet.UI.Internal.Mvc.Extensions;

namespace Revis.VClinicNet.UI.Internal.Mvc.Controllers
{
    public class WorkTimeController : Infrastructure.ControllerBase
    {
        private IWorkTimeService _workTimeService = null;

        public WorkTimeController(IWorkTimeService iwts) :
            base("WorkTime")
        {
            _workTimeService = iwts;
        }

        /// <summary>
        /// Display form to edit work time
        /// </summary>
        /// <param name="id">work time id</param>
        /// <returns></returns>
        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Recepcionist)]
        public ActionResult Edit(long id)
        {
            WorkTimeDto workTimeDto = _workTimeService.Find(id);
            if (workTimeDto == null) {
                return ErrorView("Nie znaleziono godzin pracy", "Edit");
            }
            return View(workTimeDto);
        }

        /// <summary>
        /// Edit doctor worke time
        /// </summary>
        /// <param name="workTimeDto">work time model</param>
        /// <returns></returns>
        [HttpPost]
        [HistoryBackLinked(Type = BackLinkType.ModifyForm)]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Recepcionist)]
        public ActionResult Edit(WorkTimeDto workTimeDto)
        {
            try {
                _workTimeService.Edit(workTimeDto);
            } catch (ValidationException ve) {
                ModelState.AddModelErrors(ve);
                return View(workTimeDto);
            } catch (ClinicException) { }
            return RedirectToAction("Detail", "Doctor", new { id = workTimeDto.DoctorId });
        }

        /// <summary>
        /// Display work time detail
        /// </summary>
        /// <param name="id">work time id</param>
        /// <returns></returns>
        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Recepcionist | WorkerUserRole.Doctor)]
        public ActionResult Detail(long id)
        {
            WorkTimeDto workTimeDto = _workTimeService.Find(id);
            if (workTimeDto == null) {
                return ErrorView("Nie znaleziono godzin pracy", "Edit");
            }
            return View(workTimeDto);
        }
    }
}
